# -*- coding: utf-8 -*-
'''
Created on 03/lug/2009

@author: sax
'''
from django import forms
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse, NoReverseMatch

__all__=['ReadOnlyWidget','ModelLinkWidget']

class ReadOnlyWidget(forms.HiddenInput):
    def __init__(self, admin_site, obj, field_name, attrs=None):
        self.admin_site = admin_site   
        self.attrs = attrs
        if obj:
            self.original_object = getattr(obj, field_name)        
        else:
            self.original_object = None
            
        self.original_value = str(self.original_object)        
        display_func = getattr(obj, "get_%s_display" % field_name, None) 

        if hasattr(self.original_object,'pk'):
            self.display_value = str(self.original_object)
            self.original_value = self.original_object.pk
        else:
            if display_func:
                self.display_value = display_func()
            else:        
                self.display_value = str(self.original_object)
        super(ReadOnlyWidget, self).__init__( attrs )
    
    def _has_changed(self, initial, data):
        return False
    
    def render(self, name, value, attrs=None):        
        if self.original_value is not None:
            return super(ReadOnlyWidget, self).render(name, value, attrs) +\
                    mark_safe('<pan class="xReanOnlyWidget>"%s</span>' % self.display_value)
        else:
            return "None"

    def value_from_datadict(self, data, files, name):
        return self.original_value

class ModelLinkWidget( ReadOnlyWidget ):
    
    def _get_model_change_link(self, model_instance):        
        info = ( str(self.admin_site.name), model_instance._meta.app_label, model_instance._meta.module_name )
        name='%s:%s_%s_change' % info
        try:            
            return reverse(name, args=(model_instance.pk,))
        except NoReverseMatch:
            raise NoReverseMatch("Error reversing %s" % name )
                            
    def render(self, name, value, attrs=None):
        if hasattr(self.original_object,'pk'):
            link =  self._get_model_change_link( self.original_object )
            label = unicode(self.display_value)
            return mark_safe('<a class="xModelLinkWidget" href="%s">%s</a>' % (link, label))
        else:
            return "<span class='error'>ops no pk found !!</span>"
            return super(ModelLinkWidget, self).render(name, value, attrs)
        
class URLWidget(forms.TextInput):
    def __init__(self, attrs=None):
        final_attrs = {'class': 'xURLWidget'}
        if attrs is not None:
            final_attrs.update(attrs)
        super(URLWidget, self).__init__(attrs=final_attrs)
            
    def render(self, name, value, attrs=None):
        ret = super(URLWidget, self).render(name, value, attrs)
        ret += '&nbsp;<a class="xURLWidgetLink" target="_new" href="%s">&nbsp;&nbsp;&nbsp;&nbsp;</a>' % value
        return mark_safe(ret)                
